package org.longteng.mobileluck.util;

import org.longteng.core.utils.ImageUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 图上上传base64方式的文件类
 */
public class BASE64DecodedMultipartFileUtil implements MultipartFile {
    private ImageUtil.ImgMultipartFile imgMultipartFile;

    public BASE64DecodedMultipartFileUtil(ImageUtil.ImgMultipartFile imgMultipartFile) {
        this.imgMultipartFile = imgMultipartFile;
    }

    @Override
    public String getName() {
        return imgMultipartFile.getName();
    }

    @Override
    public String getOriginalFilename() {
        return imgMultipartFile.getOriginalFilename();
    }

    @Override
    public String getContentType() {
        return imgMultipartFile.getContentType();
    }

    @Override
    public boolean isEmpty() {
        return imgMultipartFile.isEmpty();
    }

    @Override
    public long getSize() {
        return imgMultipartFile.getSize();
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgMultipartFile.getBytes();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return imgMultipartFile.getInputStream();
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        imgMultipartFile.transferTo(dest);
    }

    /**
     * 图上上传base64方式的文件类
     * @param imgBase64
     * @return sping mvc MultipartFile
     */
    public static MultipartFile base64ToMultipart(String imgBase64) {
        ImageUtil.ImgMultipartFile imgMultipartFile = ImageUtil.base64ToMultipart(imgBase64);
        return new BASE64DecodedMultipartFileUtil(imgMultipartFile);
    }
}
